<?php
/**
* @version		$Id: mod_vvisit_counter.php 2009-02-06 vinaora $
* @package		Joomla 1.0.x
* @ref			mod_digit_counter by Miro International Pty Ltd, mod_vcnt by Fliesenstadt http://www.fliesenstadt.de
* @copyright	Copyright (C) 2007 - 2009 VINAORA.COM. All rights reserved.
* @license		GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
	$time = time();
	$time = date('d-m-Y',$time);
	// Read our Parameters
	$today			=	@$params->get('today', 'Today');
	$yesterday		=	@$params->get('yesterday', 'Yesterday');
	$x_month		=	@$params->get('month', 'This month');
	$x_week			=	@$params->get('week', 'This week');
	$all			=	'Tổng truy cập';
	
	$locktime		=	@$params->get('locktime', 15);
	$initialvalue	=	@$params->get('initialvalue', 1);
	$records		=	@$params->get('records', 1);
	
	$s_today		=	@$params->get('s_today', 1);
	$s_yesterday	=	@$params->get('s_yesterday', 1);
	$s_all			=	@$params->get('s_all', 1);
	$s_week			=	@$params->get('s_week', 1);
	$s_month		=	@$params->get('s_month', 1);
	
	$s_digit		=	@$params->get( 's_digit', 1 );
	$disp_type		= 	@$params->get( 'disp_type', "Mechanical" );
	
	$widthtable		=	@$params->get( 'widthtable', "100" );
	$pretext		= 	@$params->get( 'pretext', "" );
	$posttext		= 	@$params->get( 'posttext', "" );
	
	// From minutes to seconds
	$locktime		=	$locktime * 60;
	
	$query	=	"CREATE TABLE IF NOT EXISTS #__vvisit_counter (id int(11) NOT NULL AUTO_INCREMENT, tm int NOT NULL, ip varchar(16) NOT NULL default '0.0.0.0', PRIMARY KEY (`id`))";
	$database->setQuery($query);
	$database->query();
	
	// Now we are checking if the ip was logged in the database. Depending of the value in minutes in the locktime variable.
	$day			 =	date('d');
	$month			 =	date('n');
	$year			 =	date('Y');
	$daystart		 =	mktime(0,0,0,$month,$day,$year);
	$monthstart		 =  mktime(0,0,0,$month,1,$year);
	
	// weekstart
	$weekday		 =	date('w');
	$weekday--;
	if ($weekday < 0)	$weekday = 7;
	$weekday		 =	$weekday * 24*60*60;
	$weekstart		 =	$daystart - $weekday;

	$yesterdaystart	 =	$daystart - (24*60*60);
	$now			 =	time();
	$ip				 =	$_SERVER['REMOTE_ADDR'];
	
	$query			 =	"SELECT MAX(id) FROM #__vvisit_counter";
						$database->setQuery($query);
	$all_visitors	 =	$database->loadResult();
	
	if ($all_visitors !== NULL) {
		$all_visitors += $initialvalue;
	} else {
		$all_visitors = $initialvalue;
	}
	
	// Delete old records
	$temp=$all_visitors-$records;
	
	if ($temp>0){
		$query		 =  "DELETE FROM #__vvisit_counter WHERE id<'$temp'";
						$database->setQuery($query);
						$database->query();
	}
	
	$query			 =	"SELECT COUNT(*) FROM #__vvisit_counter WHERE ip='$ip' AND (tm+'$locktime')>'$now'";
						$database->setQuery($query);
	$items			 =	$database->loadResult();
	
	if (empty($items))
	{
		$query = "INSERT INTO #__vvisit_counter (id, tm, ip) VALUES ('', '$now', '$ip')";
		$database->setQuery($query);
		$database->query();
		$e = $database->getErrorMsg();
	}
	
	$n				 = 	$all_visitors-1;
	$div = 100000;
	while ($n > $div) {
		$div *= 10;
	}

	$query			 =	"SELECT COUNT(*) FROM #__vvisit_counter WHERE tm>'$daystart'";
						$database->setQuery($query);
	$today_visitors	 =	$database->loadResult();
	
	$query			 =	"SELECT COUNT(*) FROM #__vvisit_counter WHERE tm>'$yesterdaystart' and tm<'$daystart'";
						$database->setQuery($query);
	$yesterday_visitors	 =	$database->loadResult();
		
	$query			 =	"SELECT COUNT(*) FROM #__vvisit_counter WHERE tm>='$weekstart'";
						$database->setQuery($query);
	$week_visitors	 =	$database->loadResult();

	$query			 =	"SELECT COUNT(*) FROM #__vvisit_counter WHERE tm>='$monthstart'";
						$database->setQuery($query);
	$month_visitors	 =	$database->loadResult();
	
	$content = '<div>';
	if ($pretext != "") $content .= '<div>'.$pretext.'</div>';
	// Show digit counter
	if($s_digit){		
		$content .= '<div style="text-align: center; margin-top:20px;">';
		while ($div >= 1) {
			$digit = $n / $div % 10;
			$content .= '';
			$n -= $digit * $div;
			$div /= 10;
		}
		$content .= '</div>';
	}
	
	$content		 .=	'<div><table cellpadding="0" cellspacing="0" class="visit" style="text-align: center; width: '.$widthtable.'%;"><tbody align="center">';
	// Show today, yestoday, week, month, all statistic
	if($s_today)		$content		.=	spaceer("vtoday.gif", $today, $today_visitors);
	if($s_yesterday)	$content		.=	spaceer("vyesterday.gif", $yesterday, $yesterday_visitors);
	if($s_week)			$content		.=	spaceer("vweek.gif", $x_week, $week_visitors);
	if($s_month)		$content		.=	spaceer("vmonth.gif", $x_month, $month_visitors);
	if($s_all)			$content		.=	spaceer("vall.gif", $all, $all_visitors-1);
	
	$content		.= "</tbody></table></div>";
	$content .= "</div>";	
	if ($posttext != "") $content		.= '<div>'.$posttext.'</div>';
//	$content .= '<div style="width:150px; padding-left:10px; padding-top:10px; text-align:left;">Số lượt truy cập : <b style="color:red;">'.$all_visitors.'</b></div>';

function spaceer($a1,$a2,$a3)
{
	$ret = '<tr style="text-align:left;"><td><img src="modules/mod_vvisit_counter/images/'.$a1.'" alt="mod_vvisit_counter"/></td>';
	$ret .= '<td>'.$a2.'</td>';
	$ret .= '<td style="text-align:right;"><span style="color:red;">'.$a3.'</span></td></tr>';
	return $ret;
}
?>